<?php
class UserModel
{
        public $id;
        public $username;
        public $email;
        public $password;
        public $avatar;
        public $bio;
        public $role;

        // ID
        public function getId()
        {
                return $this->id;
        }

        public function setId($id)
        {
                $this->id = $id;
                return $this; // 支持链式调用
        }

        // 用户名
        public function getUsername()
        {
                return $this->username;
        }

        public function setUsername($username)
        {
                // 可以添加验证逻辑，如用户名长度限制
                if (strlen($username) >= 3 && strlen($username) <= 20) {
                        $this->username = $username;
                } else {
                        throw new InvalidArgumentException("用户名长度必须在3-20个字符之间");
                }
                return $this;
        }

        // 邮箱
        public function getEmail()
        {
                return $this->email;
        }

        public function setEmail($email)
        {
                // 验证邮箱格式
                if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                        $this->email = $email;
                } else {
                        throw new InvalidArgumentException("邮箱格式无效");
                }
                return $this;
        }

        // 密码
        public function getPassword()
        {
                return $this->password;
        }

        public function setPassword($password)
        {
                // 可以添加密码强度验证
                $this->password = $password;
                return $this;
        }

        // 头像
        public function getAvatar()
        {
                return $this->avatar;
        }

        public function setAvatar($avatar)
        {
                $this->avatar = $avatar;
                return $this;
        }

        // 个人简介
        public function getBio()
        {
                return $this->bio;
        }

        public function setBio($bio)
        {
                // 限制简介长度
                if (strlen($bio) <= 500) {
                        $this->bio = $bio;
                } else {
                        throw new LengthException("个人简介不能超过500个字符");
                }
                return $this;
        }

        // 角色
        public function getRole()
        {
                return $this->role;
        }

        public function setRole($role)
        {
                // 假设角色只能是预设值
                $allowedRoles = ['user', 'admin', 'moderator'];
                if (in_array($role, $allowedRoles)) {
                        $this->role = $role;
                } else {
                        throw new DomainException("无效的角色值");
                }
                return $this;
        }
        public static function fromArray(array $data)
        {
                if (
                        isset($data["id"]) && is_numeric($data["id"]) &&
                        isset($data["username"]) && strlen($data["username"]) >= 3 && strlen($data["username"]) <= 20 &&
                        isset($data["email"]) && filter_var($data["email"], FILTER_VALIDATE_EMAIL) &&
                        isset($data["password"]) && strlen($data["password"])
                ) {
                        $user = new UserModel();
                        $user->setId($data["id"])
                                ->setUsername($data["username"])
                                ->setEmail($data["email"])
                                ->setPassword($data["password"])
                                ->setAvatar($data["avatar"] ?? "")
                                ->setBio($data["bio"] ?? "")
                                ->setRole($data["role"] ?? "user");
                        return $user;
                } else {
                        throw new InvalidArgumentException("数据格式错误");
                }
        }
}
